package com.cmcc.groupcontacts.util;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Intents;
import android.widget.Toast;

/**
 * Intent管理器
 * 
 * @author 
 * 
 */
public abstract class IntentManager {
	/**
	 * 启动新的Activity
	 * 
	 * @param context
	 * @param cls
	 */
	public static void startActivity(Context context, Class<?> cls) {
		Intent intent = new Intent(context, cls);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);
	}

	/**
	 * 拨打电话
	 * 
	 * @param context
	 * @param number
	 */
	public static void call(Context context, String number) {
		try {
			number = number.replace("-", "");
			Intent intent = new Intent(Intent.ACTION_CALL);
			intent.setData(Uri.fromParts("tel", number, null));
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(intent);
		} catch (Exception e) {
			Toast.makeText(context, "没有拨号权限", Toast.LENGTH_SHORT).show();
		}
	}

	/**
	 * 发送短信
	 * 
	 * @param context
	 * @param number
	 *            号码
	 * @param content
	 *            内容
	 */
	public static void sms(Context context, String number, String content) {
		try {
			Uri smsToUri = Uri.parse("smsto:" + number);
			Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intent.putExtra("sms_body", content);
			context.startActivity(intent);
		} catch (Exception e) {
			Toast.makeText(context, "没有安装短信应用", Toast.LENGTH_SHORT).show();
		}
	}

	/**
	 * 发送邮件
	 * 
	 * @param context
	 * @param emailAddr
	 */
	public static void mailTo(Context context, String emailAddr) {
		try {
			Uri mailToUri = Uri.parse("mailto:" + emailAddr);
			Intent intent = new Intent(Intent.ACTION_SENDTO, mailToUri);
			context.startActivity(intent);
		} catch (Exception e) {
			Toast.makeText(context, "没有安装邮箱应用", Toast.LENGTH_SHORT).show();
		}
	}

	/**
	 * 文本内容分享
	 * 
	 * @param context
	 * @param title
	 * @param subject
	 * @param content
	 */
	public static void shareText(Context context, String title, String subject, String content) {
		try {
			Intent intent = new Intent(Intent.ACTION_SEND);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intent.setType("text/plain");
			intent.putExtra(Intent.EXTRA_SUBJECT, subject);
			intent.putExtra(Intent.EXTRA_TEXT, content);
			context.startActivity(Intent.createChooser(intent, title));
		} catch (Exception e) {
			Toast.makeText(context, "没有安装分享应用", Toast.LENGTH_SHORT).show();
		}
	}

	/**
	 * 返回桌面
	 * 
	 * @param context
	 */
	public static void gotoHome(Context context) {
		try {
			Intent intent = new Intent(Intent.ACTION_MAIN);
			// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intent.addCategory(Intent.CATEGORY_HOME);
			context.startActivity(intent);
		} catch (Exception e) {
			Toast.makeText(context, "没有安装桌面应用", Toast.LENGTH_SHORT).show();
		}
	}

	/**
	 * 添加号码到联系人（新建或追加到已有）
	 * 
	 * @param context
	 * @param number
	 */
	public static void addToContacter(Context context, String number) {
		try {
			Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
			intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
			intent.putExtra(Intents.Insert.PHONE, number);
			context.startActivity(intent);
		} catch (Exception e) {
			try {
				Intent intent = new Intent(Intent.ACTION_INSERT);
				intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
				intent.putExtra(Intents.Insert.PHONE, number);
				context.startActivity(intent);
			} catch (Exception ex) {
				Toast.makeText(context, "没有安装通信录应用", Toast.LENGTH_SHORT).show();
			}
		}
	}
}
